//
//  ZooLogin.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/22/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ZooLogin.h"
#import <RestKit/JSONKit.h>

@implementation ZooLogin

- (id)initWithTarget:(id)target selector:(SEL)selector {
    self = [super init];
    
    if (self) {
        _target = target;
        _selector = selector;
    }
    return self;
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    if ([request isGET]) {
        if ([response isOK]) {
            NSString *myJson = [response bodyAsString];
            NSLog(@"my json: %@", myJson);
            NSDictionary *parseJSON = [myJson objectFromJSONString];
            NSString *status = [parseJSON objectForKey:STATUS];
            NSLog(@"my status: %@", status);
            
            NSString *userID = [parseJSON objectForKey:USER_ID];
            NSString *userName = [parseJSON objectForKey:USER_NAME];
            NSString *avatar = [parseJSON objectForKey:USER_AVATAR];
            NSLog(@"user_id = %@, username = %@, avatar = %@",userID, userName, avatar);
                
            _zooLoginResponse = [[ZooLoginResponse alloc]init];
            _zooLoginResponse.userID = userID;
            _zooLoginResponse.userName = userName;
            _zooLoginResponse.avatar = avatar;
            [_target performSelector:_selector withObject:_zooLoginResponse];
            
        } else if ([response isNotFound]) {
            NSString *myJson = [response bodyAsString];
            NSLog(@"my json: %@", myJson);
            NSDictionary *parseJSON = [myJson objectFromJSONString];
            NSString *status = [parseJSON objectForKey:STATUS];
            NSLog(@"my status: %@", status);
            
            NSString *error = [parseJSON objectForKey:ERROR_RESPONSE];
            NSString *error_code = [parseJSON objectForKey:ERROR_CODE_RESPONSE];
            NSLog(@"error: %@, error_code: %@", error, error_code);
        }
    }
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    
}
@end
